// FrameRect.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "JHComm.h"
#include "JHZk.h"


int main()
{
	HINSTANCE hInstComm, hInstZk;
	HANDLE hdl;
	JHINFO_SERIALPORT JHInfo_serialport;
	JHTRANSPORTLAYER * pJHTransportlayer;
	int ret;

	typedef JHZRESULT (* PJHCREATEINST)(HANDLE *, unsigned short);
	PJHCREATEINST pJHCreateInst;

	typedef JHZRESULT (* PJHMOUNTTRANSPORTLAYER)(HANDLE, LPJHTRANSPORTLAYER, const void *);
	PJHMOUNTTRANSPORTLAYER pJHMountTransportLayer;

	typedef JHZRESULT (* PJHFRAMERECT)(HANDLE, short, short, unsigned short, unsigned short);
	PJHFRAMERECT pJHFrameRect;

	typedef JHZRESULT (* PDELETEINST)(HANDLE);
	PDELETEINST pDeleteInst;

	/* Load resources jinhancomm.dll */
	hInstComm = ::LoadLibraryA("..\\..\\dll\\jinhancomm.dll");
	if (NULL == hInstComm)
	{
		printf("Failure to load resources jinhancomm.dll!");
		return -1;
	}

	/* Load  resources jinhanzk.dll */
	hInstZk = ::LoadLibraryA("..\\..\\dll\\jinhanzk.dll");
	if (NULL == hInstZk)                           
	{
		printf("Failure to load resources jinhanzk.dll!");
		FreeLibrary(hInstComm);
		return -1;
	}

	/* Create instance */
	pJHCreateInst= (PJHCREATEINST)GetProcAddress(hInstZk, "JHCreateInst");
	if (NULL == pJHCreateInst)
	{
		printf("Failure to load JHCreateInst address!");
		FreeLibrary(hInstZk);
		FreeLibrary(hInstComm);
		return -1;
	}

	(* pJHCreateInst)(&hdl, 0);
	if (NULL == hdl)
	{
		printf("\nFailure to create instance!\n");
		FreeLibrary(hInstZk);
		FreeLibrary(hInstComm);
		return -1;
	}

	/* Communication mode and parameters setting */
	JHInfo_serialport.size = sizeof (JHINFO_SERIALPORT);
	memcpy(JHInfo_serialport.serialport, "COM1", 5);
	JHInfo_serialport.baudrate = 115200;
	JHInfo_serialport.databits = 8;
	JHInfo_serialport.parity = 0;
	JHInfo_serialport.stopbits = 0;
	JHInfo_serialport.reserved = 0;
	JHInfo_serialport.timeout = 5000;

	pJHTransportlayer = (JHTRANSPORTLAYER *)GetProcAddress(hInstComm, "JHTransportlayer_serialport");
	if (NULL == pJHTransportlayer)
	{
		printf("Failure to load JHTransportlayer_serialport address!");
		ret = -1;
		goto JH_RETURN;
	}

	pJHMountTransportLayer = (PJHMOUNTTRANSPORTLAYER)GetProcAddress(hInstZk, "JHMountTransportLayer");
	if (NULL == pJHMountTransportLayer)
	{
		printf("Failure to load JHMountTransportLayer address!");
		ret = -1;
		goto JH_RETURN;
	}

	(* pJHMountTransportLayer)(hdl, pJHTransportlayer, (const void *)(&JHInfo_serialport));

	/* Frame rect operation*/
	pJHFrameRect= (PJHFRAMERECT)GetProcAddress(hInstZk, "JHFrameRect");
	if (NULL == pJHFrameRect)
	{
		printf("Failure to load JHFrameRect address!");
		ret = -1;
		goto JH_RETURN;
	}

	JHZRESULT res = (* pJHFrameRect)(hdl, 0, 0, 32, 16);
	if (JR_OK == res)
	{
		printf("\nSuccessed in framing rect!\n");
		ret = 0;
	}
	else
	{
		printf("\nFailure to frame rect!\n");
		ret = -1;
	}

JH_RETURN:
	/* Delete instance */
	pDeleteInst= (PDELETEINST)GetProcAddress(hInstZk, "JHDeleteInst");
	if (pDeleteInst)
	{
		(* pDeleteInst)(hdl);
	}

	/* Free resources jinhanzk.dll */
	FreeLibrary(hInstZk);

	/* Free resources jinhancomm.dll */
	FreeLibrary(hInstComm);

	Sleep(2000);

	return ret;
}

